package com.mingqijia.gassafety.job.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mingqijia.gassafety.authority.dal.handler.ConfigureTranslator;
import com.mingqijia.gassafety.authority.dal.provider.DictionaryProvider;
import com.mingqijia.gassafety.db.entity.Organization;
import com.mingqijia.gassafety.db.mapper.OrganizationMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mingqijia.gassafety.job.response.OrganizationResponse;
import com.mingqijia.gassafety.job.service.OrganizationService;
import com.mingqijia.gassafety.shared.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 组织层级关系 服务实现类
 * </p>
 *
 * @author li.shiying
 * @since 2023-07-24
 */
@Service
@Slf4j
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService {

    @Autowired
    OrganizationMapper organizationMapper;
    @Autowired
    DictionaryProvider dProvider;
    @Autowired
    ConfigureTranslator translator;

    @Override
    public List<OrganizationResponse> getList(String spId) {
        LambdaQueryWrapper<Organization> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Organization::getParentId,spId).eq(Organization::getIsDeleted,0);
        List<Organization> list = this.list(queryWrapper);
        if (!CollectionUtil.isEmpty(list)){
            List<OrganizationResponse> collect = list.stream().map(item -> {
                OrganizationResponse response = new OrganizationResponse();
                BeanUtils.copyProperties(item, response);
                return response;
            }).collect(Collectors.toList());
            //上层组织的信息
            OrganizationResponse response = new OrganizationResponse();
            response.setSpId(list.get(0).getParentId());
            response.setSpName(list.get(0).getParentName());
            collect.add(response);
            return collect;
        }
        return null;
    }

    /**
     * 获取上级组织spid
     * @param spId
     * @return
     */
    @Override
    public List<Organization> getParentIdBySpid(String spId) {
        return  organizationMapper.getParentIdBySpid(spId);
    }

    /**
     * 查询子组织和自己的spId列表
     * 如果有子组织，返回自己和自组织spId列表
     * 如果无子组织，返回自己spId列表
     * @param spId
     * @return
     */
    @Override
    public List<String> getSpIds(String spId) {
        List<String> spIds = new ArrayList<>();
        spIds.add(spId);
        LambdaQueryWrapper<Organization> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Organization::getParentId,spId).eq(Organization::getIsDeleted,0);
        List<Organization> list = this.list(queryWrapper);
        if (!CollectionUtil.isEmpty(list)) {
            for (Organization organization : list) {
                spIds.add(organization.getSpId());
            }
        }
        return spIds;
    }

    @Override
    public boolean hasChildCompany(String spId) {
        List<String> childList = organizationMapper.getChildList(spId);
        if (!CollectionUtil.isEmpty(childList)){
            return true;
        }
        return false;
    }
}